package com.anti.modular.bus.billrentadjustment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.core.utils.DecimalUtils;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.billrentadjustment.entity.BillRentAdjustment;
import com.anti.modular.bus.billrentadjustment.enums.BillRentAdjustmentExceptionEnum;
import com.anti.modular.bus.billrentadjustment.param.BillRentAdjustmentParam;
import com.anti.modular.bus.billrentadjustment.service.BillRentAdjustmentService;
import com.anti.modular.bus.housebill.service.HouseBillService;
import com.anti.modular.bus.housebilldetail.service.HouseBillDetailService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 租金调整记录service接口实现类
 *
 * @author anti
 * @date 2021-05-10 14:46:45
 */
@Service
public class BillRentAdjustmentServiceImpl implements BillRentAdjustmentService {
    @Resource
    DBUtils dbUtils;
    @Resource
    HouseBillDetailService houseBillDetailService;

    @Override
    public BillRentAdjustment getById(BillRentAdjustmentParam billRentAdjustmentParam){
        String sql="select * from bill_rent_adjustment where id =? ";
        return dbUtils.get(sql,BillRentAdjustment.class,billRentAdjustmentParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,BillRentAdjustmentParam billRentAdjustmentParam) {
        StringBuilder sb=new StringBuilder("select * from bill_rent_adjustment where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(billRentAdjustmentParam)) {
            // 根据期数数字 查询
            if (ObjectUtil.isNotEmpty(billRentAdjustmentParam.getQsNum())) {
               sb.append(" and qsnum = ?");
               param.add(billRentAdjustmentParam.getQsNum());
            }
            // 根据生效账单名稱 查询
            if (ObjectUtil.isNotEmpty(billRentAdjustmentParam.getName())) {
               sb.append(" and name = ?");
               param.add(billRentAdjustmentParam.getName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BillRentAdjustment> list(BillRentAdjustmentParam billRentAdjustmentParam) {
        String sql="";
        return dbUtils.query(sql,BillRentAdjustment.class);
    }

    @Override
    public List<BillRentAdjustment> getAdjustmentListByLeaseId(Long leaseId) {
        String sql="select *  from bill_rent_adjustment where lease_id =?";
        return dbUtils.query(sql,BillRentAdjustment.class,leaseId);
    }

    @Override
    public void add(BillRentAdjustmentParam billRentAdjustmentParam) {
        BillRentAdjustment billRentAdjustment = new BillRentAdjustment();
        BeanUtil.copyProperties(billRentAdjustmentParam, billRentAdjustment);
        Integer adjustStyle = billRentAdjustment.getAdjustStyle();
        Integer adjustType = billRentAdjustment.getAdjustType();
        BigDecimal rent = billRentAdjustment.getRent();
        BigDecimal changeMoney = billRentAdjustment.getChangeMoney();
        BigDecimal decimal = makeMoney(adjustType, adjustStyle, rent, changeMoney);
        billRentAdjustment.setRent(decimal);
        billRentAdjustment.setStatus(0);
        billRentAdjustment.setCreateTime(new Date());
        billRentAdjustment.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(billRentAdjustment);
        Integer qsNum = billRentAdjustmentParam.getQsNum();
        Long leaseId = billRentAdjustmentParam.getLeaseId();
        houseBillDetailService.editBillDetailByLeaseIdAndQsNum(leaseId,qsNum,decimal);
    }
    private BigDecimal makeMoney(int adjustType, int adjustStyle, BigDecimal rent, BigDecimal changeMoney){
        BigDecimal rentPrice = new BigDecimal(0.00);
        if (adjustType == 2) {
            if (adjustStyle == 1) {
                rentPrice = changeMoney;
            } else {
                rentPrice = DecimalUtils.multiply(rent,DecimalUtils.divide(changeMoney,new BigDecimal(100)));
            }
        } else {
            if (adjustStyle == 1) {
                rentPrice = changeMoney.negate();
            } else {
                rentPrice = DecimalUtils.multiply(rent,DecimalUtils.divide(changeMoney,new BigDecimal(100))).negate();
            }
        }
        return DecimalUtils.add(rentPrice,rent);
    }

    public void delete(BillRentAdjustmentParam billRentAdjustmentParam) {
        String sql="delete  from bill_rent_adjustment where id =? ";
        dbUtils.execSql(sql,billRentAdjustmentParam.getId());
    }

    @Override
    public void deleteAdjustById(Long id) {
        String sql="delete  from bill_rent_adjustment where id =? ";
        dbUtils.execSql(sql,id);
    }

    @Override
    public void deleteAdjustByIdOnEditZk(Long leaseId, Integer qsNum) {
        String sql="delete  from bill_rent_adjustment where lease_id =? and qs_num>=? and qs_num <>1";
        dbUtils.execSql(sql,leaseId,qsNum);
    }

    public void edit(BillRentAdjustmentParam billRentAdjustmentParam) {
        BillRentAdjustment billRentAdjustment = this.queryBillRentAdjustment(billRentAdjustmentParam);
        BeanUtil.copyProperties(billRentAdjustmentParam, billRentAdjustment);
        dbUtils.update(billRentAdjustment);
    }

    @Override
    public BillRentAdjustment detail(BillRentAdjustmentParam billRentAdjustmentParam) {
        return this.queryBillRentAdjustment(billRentAdjustmentParam);
    }

    @Override
    public BillRentAdjustment getAdjustAmountByLeaseIdOnQsNum(Long leaseId, Integer qsNum) {
        String sql="select * from bill_rent_adjustment where  id=(select MAX(id)  from bill_rent_adjustment where  lease_id =? and qs_num<=?) ";
        return dbUtils.getFirst(sql,BillRentAdjustment.class,leaseId,qsNum);
    }

    /**
     * 获取租金调整记录
     *
     * @author anti
     * @date 2021-05-10 14:46:45
     */
    private BillRentAdjustment queryBillRentAdjustment(BillRentAdjustmentParam billRentAdjustmentParam) {
        BillRentAdjustment billRentAdjustment = this.getById(billRentAdjustmentParam);
        if (ObjectUtil.isNull(billRentAdjustment)) {
            throw new BusinessException(BillRentAdjustmentExceptionEnum.NOT_EXIST);
        }
        return billRentAdjustment;
    }


}
